from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework.permissions import IsAdminUser
from datetime import date
from datetime import timedelta

from apps.users.models import User
from apps.goods.models import GoodsVisitCount
from apps.meiduo_admin.serializers.home import CategorySerializer


#########用户总数统计##############
class TotaUserCountAPIView(APIView):

    # permission_classes = [IsAdminUser]

    def get(self,request):
        count = User.objects.all().count()
        return Response({"count":count})


#########日增用户统计##############
class DailyUserAddCountAPIView(GenericAPIView):

    # permission_classes = [IsAdminUser]

    def get(self,request):
        # 查询注册用户的注册日期 大于等于今天就可以了
        # 1. 获取当天的日期
        # date
        # 2020-02-13
        # datetime  last_login
        # 2020-02-13 00:00:00
        now_today = date.today()

        # 2. 过滤查询
        count = User.objects.filter(date_joined__gte=now_today).count()

        return Response({
            "count":count,
        })


#########日活用户统计##############
class UserDailyActiveCountView(GenericAPIView):

    # permission_classes = [IsAdminUser]

    def get(self,request):
        # 查询注册用户的注册日期 大于等于今天就可以了
        # 1. 获取当天的日期
        now_today = date.today()

        # 2. 过滤查询
        count = User.objects.filter(last_login__gte=now_today).count()

        return Response({
            "count":count,
        })

#########日下单用户统计##############
class UserDailyOrderCountAPIView(APIView):

    # permission_classes = [IsAdminUser]

    def get(self,request):

        today = date.today()

        #
        count = User.objects.filter(orderinfo__create_time__gte=today).distinct().count()
        return Response({
            "count":count
        })


#########月增用户统计##############
class MonthUserCountAPIView(APIView):

    # permission_classes = [IsAdminUser]

    def get(self,request):
        """

            01.01   20
            01.02   22
            01.03   23
            01.04   27
            ...
        :param request:
        :return:
        """
        # 1. 获取当天的日期      01.31
        today = date.today()

        # 2. 获取30天前的日期    01.01
        start_date = today - timedelta(days=30)

        date_list = []
        # 3. 再遍历获取每一天的 用户量
        for i in range(30):
            begin_date = start_date + timedelta(days=i) # 1.1
            end_date = start_date + timedelta(days=i+1)

            count = User.objects.filter(date_joined__gte=begin_date,
                                        date_joined__lt=end_date).count()

            date_list.append({
                "date":begin_date,
                "count":count
            })

        return Response(date_list)


#########日分类商品的访问量##############
# class CategoryAPIView(APIView):

    # def get(self,request):
    #     # 1. 获取日期
    #     today = date.today()
    #     # 2. 查询数据--> 指定查询集
    #     goods = GoodsVisitCount.objects.filter(date__gte=today)
    #     # 3.    对象列表  转  字典列表
    #     serializer = CategorySerializer(goods, many=True)
    #     #　４．　返回响应
    #     return Response(serializer.data)

# 查询多个数据,继承 ListAPIView 测试
class CategoryAPIView(ListAPIView):

    # 指定序列化器
    serializer_class = CategorySerializer
    # 指定查询集
    queryset = GoodsVisitCount.objects.filter(date=date.today())